import Cookies from 'js-cookie'
import _tools from '@/utils/_tools';
import { re } from 'mathjs';

const TokenKey = 'token'
const UserLogin = 'user_login'
const UserInfoKey = 'userInfo';
export function getToken() {
  return localStorage.getItem(TokenKey)
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
}

export function removeToken() {
  localStorage.removeItem(UserLogin)
  return localStorage.removeItem(TokenKey)
}

export function setUserInfo(userInfo) {
  userInfo = JSON.stringify(userInfo);
  userInfo = _tools.encrypt(userInfo);
  return localStorage.setItem(UserInfoKey, userInfo)
}

export function getUserInfo() {
  let userInfo = localStorage.getItem(UserInfoKey)
  if (userInfo) {
    userInfo = JSON.parse(_tools.decode_q(userInfo))
  }
  return userInfo
}

export function loginState() {
  let userInfo = getUserInfo()
  if (!userInfo) return false;
  let newTime = Date.parse(new Date()) / 1000;
  // 一天
  if (userInfo.loginTime + 86400 > newTime) {
    return true;
  } else {
    return false;
  }
}

// 文件判断显示预览图
export function fileImgFunc(file) {
  // 针对新增工程整改单的修改（图片）
  if (file.url && file.raw) {
    file.file_url = URL.createObjectURL(file.raw);
  }
  file.name = file.name || file.file_name;
  let fileName = file.raw ? file.raw.name : file.file_name;
  var fileExtension = file.file_suffix || fileName.substring(fileName.lastIndexOf('.') + 1);

  // console.log('File format:', fileExtension);
  let word = require('@/assets/file_icon/word.png');
  let cad = require('@/assets/file_icon/cad.png');
  let excel = require('@/assets/file_icon/excel.png');
  let pdf = require('@/assets/file_icon/pdf.png');
  let ppt = require('@/assets/file_icon/ppt.png');
  let zip = require('@/assets/file_icon/zip.png');
  let other = require('@/assets/file_icon/other.png');
  let video = require('@/assets/file_icon/video.png');
  switch (fileExtension) {
    case 'doc':
      file.src = word;
      break;
    case 'docx':
      file.src = word;
      break;
    case 'pptx':
      file.src = ppt;
      break;
    case 'pptm':
      file.src = ppt;
      break;
    case 'ppt':
      file.src = ppt;
      break;
    case 'pdf':
      file.src = pdf;
      break;
    case 'xls':
      file.src = excel;
      break;
    case 'xlsx':
      file.src = excel;
      break;
    case 'zip':
      file.src = zip;
      break;
    case 'rar':
      file.src = zip;
      break;
    case 'dwg':
      file.src = cad;
      break;
    case 'dxf':
      file.src = cad;
      break;
    case 'dwt':
      file.src = cad;
      break;
    case 'dwf':
      file.src = cad;
      break;
    default:
      let img_type_arr = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'];
      let video_type_arr = ['mp4', 'avi', 'mkv', 'rmvb', 'MOV', 'm4v']

      if (img_type_arr.indexOf(fileExtension) != -1) {
        file.src = file.file_url || file.url;
      } else if (video_type_arr.indexOf(fileExtension) != -1) {
        file.src = video
      } else {
        file.src = other
      }

  }
  return file.src;
}

// 文件判断显示预览图
export function filePostImgFunc(file) {
  console.log(file, 'zsadaseqwe')
  // console.log(file, '文件判断显示预览图')
  let fileName = '';
  fileName = file.raw ? file.raw.name : file.file_name;
  var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
  // 统一数据用 （线上文件，跟本地文件）
  file.type = file.file_suffix;
  file.name = file.name ? file.name : file.file_name;

  // console.log('File format:', fileExtension);
  let word = require('@/assets/file_icon/word.png');
  let cad = require('@/assets/file_icon/cad.png');
  let excel = require('@/assets/file_icon/excel.png');
  let pdf = require('@/assets/file_icon/pdf.png');
  let ppt = require('@/assets/file_icon/ppt.png');
  let zip = require('@/assets/file_icon/zip.png');
  let other = require('@/assets/file_icon/other.png');
  let video = require('@/assets/file_icon/video.png');
  switch (fileExtension) {
    case 'doc':
      file.src = word;
      break;
    case 'docx':
      file.src = word;
      break;
    case 'pptx':
      file.src = ppt;
      break;
    case 'pptm':
      file.src = ppt;
      break;
    case 'ppt':
      file.src = ppt;
      break;
    case 'pdf':
      file.src = pdf;
      break;
    case 'xls':
      file.src = excel;
      break;
    case 'xlsx':
      file.src = excel;
      break;
    case 'zip':
      file.src = zip;
      break;
    case 'dwg':
      file.src = cad;
      break;
    case 'dxf':
      file.src = cad;
      break;
    case 'dwt':
      file.src = cad;
      break;
    case 'dwf':
      file.src = cad;
      break;
    default:
      let type = file.type.split('/')
      let type_arr = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'mp4', 'avi', 'mkv', 'rmvb', 'quicktime'];

      if (type[1]) {
        if (type_arr.indexOf(type[1]) != -1) {
          file.src = file.src || file.url;
        } else {
          file.src = other
        }
      } else {
        if (type_arr.indexOf(file.type) != -1) {
          file.src = file.src || file.url;
        } else {
          file.src = other
        }
      }
  }
  return file;
}
// 获取视频文件封面图(线上链接数据)
export function videoPostOnline(file, callback) {
  // console.log(isVideoFile(file), "是否视频")
  file.src = file.file_url;//根据后端返回值设定
  file.type = file.file_suffix;//根据后端返回值设定
  if (!isVideoFile(file)) {
    callback(filePostImgFunc(file))
    return;
  }
  let videoUrl = file.file_url + '?v=' + Date.now();
  // console.log(videoUrl, "videoUrl视频路径")
  // 创建video元素
  const video = document.createElement('video');
  video.src = videoUrl;

  // 监听video加载完成
  video.setAttribute('crossOrigin', 'anonymous'); //处理跨域
  video.currentTime = 1;//第一帧
  video.onloadeddata = function () {
    // 创建canvas元素
    const canvas = document.createElement('canvas');
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;

    // 获取canvas上下文
    const ctx = canvas.getContext('2d');
    ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

    // 将canvas转换为图片并回调
    const imageUrl = canvas.toDataURL('image/png');
    file.src = imageUrl;
    callback(filePostImgFunc(file));
  };
}


// 获取视频文件封面图(文件流)
export function videoPost(file, callback) {
  // console.log(isVideoFile(file.raw), "是否视频")
  file.file_suffix = file.raw.type;//根据后端返回值设定
  if (!isVideoFile(file.raw)) {
    callback(filePostImgFunc(file))
    return;
  }
  let videoUrl = file.url;
  // 创建video元素
  const video = document.createElement('video');
  video.src = videoUrl;

  // 监听video加载完成
  video.currentTime = 1;//第一帧
  video.onloadeddata = function () {
    // 创建canvas元素
    const canvas = document.createElement('canvas');
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;

    // 获取canvas上下文
    const ctx = canvas.getContext('2d');
    ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
    // 将canvas转换为图片并回调
    const imageUrl = canvas.toDataURL('image/png');
    file.src = imageUrl;
    callback(filePostImgFunc(file));
  };
}
// 是否视频判断
export function isVideoFile(file) {
  const videoMimeTypes = [
    'video/mp4',
    'video/webm',
    'video/ogg',
    'video/quicktime',
    // 添加其他视频MIME类型
    'mp4',
    'webm',
    'ogg',
    'avi',
    'mkv',
    'rmvb'
  ];
  return videoMimeTypes.includes(file.type);
}

// 时间转换成日期
export function dateTimeToDate(dateTime) {
  let dateArr = dateTime.match(/\d{4}-\d{2}-\d{2}/);
  return dateArr[0];
}

export function msgJump(mark, item) {
  let path = '';
  let query = '';
  switch (mark) {
    case 480://客户经理提交定金
      path = '/collectionPaymentSkdetailPage';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 485://拒绝定金
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 490://同意定金
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        // to_do: '1'
      }
      break;
    case 499://财务驳回--增项款待标记
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;

    case 510://客户创建项目(需要分配客户经理)--客户总监
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 520://客户总监分配客户经理(需要完善项目)--客户经理
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 530://提前进场  分配工程经理
      if (getUserInfo().position_id == 2) {
        path = '/designManagement';
      } else {
        path = '/constructionManagement';
      }
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 535://提交勘场费
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '2'
      }
      break;
    case 540://客户经理提交勘场费用(需要确认勘场费用信息)--财务
      path = '/collectionPaymentSkdetailPage';
      query = {
        id: item.object_id,
        examine: true
        // project_name: item.project_name,
        // to_do: '1'
      }
      break;
    case 545://财务拒绝勘场费
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 550://财务确认勘场费用(需要分配勘场工程经理)--工程部经理
      path = '/siteSurveyManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 560://工程部经理分配完勘场工程经理(需要提交勘场报告)--工程经理
      path = '/siteSurveyManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 570://工程经理提交勘场报告(需要提交勘场资料)--设计师
      path = '/siteSurveyManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 580://设计师提交勘场资料(需要上传设计图)--设计师，造价员，客户经理
      path = '/designManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 590://上传完设计图(需要申请报价)--客户经理
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 600://分配施工工程经理--工程部经理
      path = '/constructionManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 605://分配造价员--造价主管
      path = '/budgetManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 610://出具造价单--造价员
      path = '/budgetManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 620://工程部经理分配了工程经理--工程经理无需操作
      path = '/constructionManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 625://工程经理拒绝指派
      path = '/constructionManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 630://审核造价单--工程经理
      path = '/budgetDetailPage';
      query = {
        id: item.project_id,
        examine: true
      }
      break;
    case 640://审核拒绝造价单--造价员
      path = '/budgetManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 650://工程经理通过造价单
      path = '/budgetDetailPage';
      query = {
        id: item.project_id,
        examine: true
      }
      break;
    case 660://造价主管通过造价单
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 670://客户经理对客报价
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 680://客户经理发起优惠审批
      path = '/discountDetail';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 682://客户总监审核拒绝
      path = '/discountApproval';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 684://客户总监审核通过，等待总经理审批
      path = '/discountDetail';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 686://总经理审批拒绝
      path = '/discountApproval';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 688://总经理审批同意，审核完成
      path = '/discountApproval';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 695://客户经理去提交付款信息
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '2'
      }
      break;
    case 700://提交预付款
      path = '/collectionPaymentSkdetailPage';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 705://财务拒绝预付款
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 710://财务确认预付款
      path = '/constructionManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 720://现场启动
      path = '/qualityAcceptance';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 730://提交节点验收
      path = '/qualityAcceptance';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 735://节点提交进度款
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        // to_do: '1'
      }
      break;
    case 740://工程监理质量验收审核
      path = '/qualityAcceptance';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 750://工程部经理质量验收审核
      path = '/qualityAcceptance';
      query = {
        project_name: item.project_name,
        // to_do: '0'
      }
      break;
    case 752://确认增减项信息
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 755://质量评分
      path = '/constructionManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 762://工程监理发起整改单
      path = '/rectificationFormDetailPage';
      query = {
        id: item.object_id,
        submit: true
      }
      break;
    case 763://工程经理提交整改内容
      path = '/rectificationFormDetailPage';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 764://工程监理整改单审核不通过
      path = '/rectificationForm';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 765://工程监理整改单审核通过
      path = '/rectificationForm';
      query = {
        project_name: item.project_name,
        // to_do: '1'
      }
      break;
    case 770://提交进度款
      path = '/collectionPaymentSkdetailPage';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 775://拒绝进度款
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 780://确认进度款(节点验收完毕)
      path = '/qualityAcceptance';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 785://施工完成
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 790://确认增减项
      path = '/settlementManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 800://出具结算单
      path = '/settlemenDetailPage';
      query = {
        // project_name: item.project_name,
        // to_do: '1'
        id: item.project_id,
        examine: true
      }
      break;
    case 810://拒绝结算单
      path = '/settlementManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 820://同意结算单

      path = '/settlemenDetailPage';
      query = {
        // project_name: item.project_name,
        // to_do: '1'
        id: item.project_id,
        examine: true
      }
      // if (getUserInfo().position_id == 2) {
      //   path = '/settlementManagement';
      //   query = {
      //     // project_name: item.project_name,
      //     // to_do: '1'
      //     id: item.project_id,
      //     examine: true
      //   }
      // }
      // if (getUserInfo().position_id == 4) {
      //   path = '/settlementManagement';
      //   query = {
      //     project_name: item.project_name,
      //     to_do: '1'
      //   }
      // }
      break;
    case 825://结算单通过，客户经理去提交尾款信息
      path = '/projectManagement';
      query = {
        project_name: item.project_name,
        to_do: '2'
      }
      break;
    case 830://提交尾款
      path = '/collectionPaymentSkdetailPage';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 834://同意优惠
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 838://拒绝尾款
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 840://同意尾款
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        // to_do: '1'
      }
      break;
    case 844://总经理审核
      path = '/collectionPaymentSkdetailPage';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 862://上传勘场资料
      path = '/siteSurveyManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    // **************增减项*********************
    case 900://待报价
      path = '/increaseDecreaseItemsList';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 905://待报价
      path = '/increaseDecreaseItemsList';
      query = {
        project_name: item.project_name,
        // to_do: '1'
      }
      break;
    case 910://重新分配造价员
      path = '/increaseDecreaseItemsList';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 920://出具增减项报价
      path = '/increaseDecreaseItemsInfo';
      query = {
        sn: item.object_id,
        examine: true
      }
      break;
    case 930://报价拒绝
      path = '/increaseDecreaseItemsList';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 940://增减项报价工程经理同意
      path = '/increaseDecreaseItemsInfo';
      query = {
        sn: item.object_id,
        examine: true
      }
      break;
    case 950://提交增减项支付
      path = '/collectionPaymentSkdetailPage';
      query = {
        id: item.object_id,
        examine: true
      }
      break;
    case 955://拒绝增减项支付
      path = '/increaseDecreaseItemsList';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 960://同意增减项支付
      path = '/increaseDecreaseItemsList';
      query = {
        project_name: item.project_name,
        // to_do: '1'
      }
      break;
    // *************售后******************
    case 970://客户经理发起售后
      path = '/afterSalesManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 972://客户发起售后
      path = '/afterSalesManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 975://分配工程经理
      path = '/afterSalesManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 980://工程经理反馈现场情况
      path = '/afterSalesManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 985://工程监理处理反馈
      path = '/afterSalesManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    case 990://工程经理提交完成情况
      path = '/afterSalesManagement';
      query = {
        project_name: item.project_name,
        to_do: '1'
      }
      break;
    // //***********付款单*********** */
    case 1101:
      path = '/collectionPaymentFkdetailPage';
      query = {
        // project_name: item.project_name,
        // activeName: '2',
        // to_do: '1'
        id: item.object_id,
        examine: true
      }
      break;
    case 1102:
      path = '/collectionPaymentManagement';
      query = {
        project_name: item.project_name,
        activeName: '2',
        to_do: '1'
      }
      break;
    case 1103:
      path = '/collectionPaymentFkdetailPage';
      query = {
        // project_name: item.project_name,
        // activeName: '2',
        // to_do: '1'
        id: item.object_id,
        examine: true
      }
      break;
    case 1104:
      path = '/collectionPaymentFkdetailPage';
      query = {
        // project_name: item.project_name,
        // activeName: '2',
        // to_do: '1'
        id: item.object_id,
        examine: true
      }
      break;
    case 1105:
      path = '/collectionPaymentFkdetailPage';
      query = {
        // project_name: item.project_name,
        // activeName: '2',
        // to_do: '1'
        id: item.object_id,
        examine: true
      }
      break;
    default:
  }
  if (!path) return;
  this.$store.commit('isEventTriggeredFunc', true);
  this.$router.push({
    path,
    query
  })
}

export function padNumberToFiveDigits(text, num) {
  // 将数字转换为字符串
  const numStr = String(num);
  // 使用 padStart 方法在前面补零，直到字符串长度为 5
  return text + numStr.padStart(5, '0');
}